package com.dlc.felear.xiaoensale.customView;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dlc.felear.xiaoensale.R;


public class LoadingDialog implements Handler.Callback {
    LVCircularRing mLoadingView;
    Dialog mLoadingDialog;
    private boolean isShow=false;
    private TextView loadingText;
    private Handler handler;

    public LoadingDialog(Context context, String msg) {
        View view = LayoutInflater.from(context).inflate(
                R.layout.loading_dialog_view, null);
        LinearLayout layout = (LinearLayout) view.findViewById(R.id.dialog_view);
        mLoadingView = (LVCircularRing) view.findViewById(R.id.lv_circularring);
        loadingText = (TextView) view.findViewById(R.id.loading_text);
        loadingText.setText(msg);
        mLoadingDialog = new Dialog(context, R.style.loading_dialog);
        mLoadingDialog.setCancelable(true);
        mLoadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        handler=new Handler(this);
    }

    public void show(String msg) {
        if (mLoadingDialog != null) {
            mLoadingDialog.show();
        }
        if (mLoadingView != null) {
            mLoadingView.startAnim();
        }

    }

    public void setMsg(String msg){
        loadingText.setText(msg);
    }

    public void show() {
        if (mLoadingDialog != null) {
            mLoadingDialog.show();
        }
        if (mLoadingView != null) {
            mLoadingView.startAnim();
        }

    }

    public void show(int delayClose) {
        show();
        handler.sendEmptyMessageDelayed(1245,delayClose);
    }

    public void close() {
        isShow=false;
        mLoadingDialog.dismiss();
    }

    @Override
    public boolean handleMessage(Message message) {
        close();
        return false;
    }
}
